package com.wiz.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.json.JSONObject;
import org.springframework.stereotype.Service;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;
import com.wiz.common.Constant;
import com.wiz.service.NoteService;
import com.wiz.util.DateUtil;
import com.wiz.util.HttpUtil;
import com.wiz.util.PageView;
import com.wiz.vo.NoteVO;
@Service("noteService")
public class NoteServiceImpl implements NoteService {
	private Gson gson = new GsonBuilder().disableHtmlEscaping().create();
	private HttpUtil httpUtil = new HttpUtil();
	
	@Override
	public String charge(NoteVO note) throws Exception {
		
		Map<String,String> params = new HashMap<String,String>();
		
		params.put("HQId",note.getHQId());
		params.put("addCnt",String.valueOf(note.getAddCnt()));
		params.put("amount",String.valueOf(note.getAmount()));
		params.put("adminId",String.valueOf(note.getAdminId()));
		params.put("adminName",note.getAdminName());
		
		
		String response = httpUtil.getHttpsResponseByPost(Constant.NOTE_CHARGE_URL, params, "utf-8");
		System.out.println(response);
    	Map<String,String> map = gson.fromJson(response,new TypeToken<Map<String,String>>() {}.getType());
    	
    	int code =  Integer.valueOf(map.get("code"));
    	if(code != 0){
    		throw new Exception(map.get("msg"));
    	}
		return map.get("msg");
	}

	@Override
	public PageView listSendHistory(String hqId,PageView pageView,String startTime,String endTime) throws Exception {
		List<NoteVO>list = new ArrayList<NoteVO>();
		Map<String,String> params = new HashMap<String,String>();
		
		params.put("HQId",hqId);
		params.put("pn",String.valueOf(pageView.getPageNow()));
		params.put("ps",String.valueOf(pageView.getPageSize()));
		if(startTime != null && !startTime.equals("")){
			params.put("startTime",DateUtil.formatDateTime(DateUtil.toDate(startTime)));
		}
		if(endTime != null && !endTime.equals("")){
			params.put("endTime",DateUtil.formatDateTime(DateUtil.toDate(endTime)));
		}
		
		String result = httpUtil.getHttpsResponseByPost(Constant.LIST_SEND_NOTE, params, "utf-8");
		JSONObject json = new JSONObject(result);
		if(!json.isNull("code")){
			String codes = json.getString("code");
			if(codes.equals("200")){
				JSONObject data = json.getJSONObject("data");
				String str = data.getString("list");
				list = gson.fromJson(str,new TypeToken<List<NoteVO>>(){}.getType());
				pageView.setRowCount(json.getInt("recordCnt"));
				pageView.setRecords(list);
			}
		}
		return pageView;
	}
	/**充值记录*/
	@Override
	public PageView listChargeHistory(String hqId,PageView pageView,String startTime,String endTime) throws Exception {
		List<NoteVO>list = new ArrayList<NoteVO>();
		Map<String,String> params = new HashMap<String,String>();
		
		params.put("HQId",hqId);
		params.put("pn",String.valueOf(pageView.getPageNow()));
		params.put("ps",String.valueOf(pageView.getPageSize()));
		if(startTime != null && !startTime.equals("")){
			params.put("startTime",DateUtil.formatDateTime(DateUtil.toDate(startTime)));
		}
		if(endTime != null && !endTime.equals("")){
			params.put("endTime",DateUtil.formatDateTime(DateUtil.toDate(endTime)));
		}
		
		String result = httpUtil.getHttpsResponseByPost(Constant.LIST_CHARGE_NOTE, params, "utf-8");
		JSONObject json = new JSONObject(result);
		if(!json.isNull("code")){
			String codes = json.getString("code");
			if(codes.equals("200")){
				JSONObject data = json.getJSONObject("data");
				String str = data.getString("list");
				list = gson.fromJson(str,new TypeToken<List<NoteVO>>(){}.getType());
				pageView.setRowCount(json.getInt("recordCnt"));
				pageView.setRecords(list);
			}
		}
		return pageView;
	}

}
